package com.service;
import static com.def.GenericDef.ACTION_FLG_ADD;
import static com.def.GenericDef.ACTION_FLG_CANCEL;
import static com.def.GenericDef.ACTION_FLG_DELETE;
import static com.def.GenericDef.ACTION_FLG_QUERY;
import static com.def.GenericDef.ACTION_FLG_UPDATE;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisQrsDao;
import com.icim.service.ICIMBaseService;
import com.tx.Xpbisqrs.XpbisqrsI;
import com.tx.Xpbisqrs.XpbisqrsO;
import com.util.MqOpe;

@Scope("prototype")
@Service("XpbisqrsService")
public class XpbisqrsService extends ICIMBaseService{
	private Logger logger = Logger.getLogger(XpbisqrsService.class);
	@Autowired
	private XpbisqrsI xpbisqrsI;
	
	@Autowired
	private XpbisqrsO xpbisqrsO; 
	
	
	@Autowired
	private IBisQrsDao bisQrsDao;
	
	@Autowired
	private MqOpe mqOpe;
	
	public String subMainProc(String strInMsg){
		logger.info("InTrx is "+ strInMsg );
		try{
			xpbisqrsI = (XpbisqrsI) mqOpe.inStr2Obj(strInMsg,xpbisqrsI);
			char action_flg = xpbisqrsI.getAction_flg().charAt(0);
			switch( action_flg ){
				case ACTION_FLG_QUERY   : xpbisqrsO = queryByConditionFnc()  ; break;
				case 'L'                : xpbisqrsO = queryQrsKeyListFnc()   ; break;
				case ACTION_FLG_ADD     : xpbisqrsO = addFnc()            ; break;
				case ACTION_FLG_UPDATE  : xpbisqrsO = updateFnc()         ; break;
				case ACTION_FLG_DELETE  : xpbisqrsO = deleteFnc()         ; break;
				case ACTION_FLG_CANCEL  : xpbisqrsO = deleteByConditionFnc(); break;
				default                 : xpbisqrsO = errActionFnc();
				
			}
			Session session = bisQrsDao.getCrSession();
		    if( RETURN_CODE_OK.equals(xpbisqrsO.getRtn_code()) ){
		    	session.flush();
			}else{
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}		
		}catch(Exception ex){
			xpbisqrsO.setTrx_id("XPBISQRS");
			xpbisqrsO.setType_id("O");
			xpbisqrsO.setRtn_code(RETURN_CODE_UNKNOWN);
			xpbisqrsO.setRtn_mesg(ex.getCause().toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}finally{
			logger.info(mqOpe.obj2Str(xpbisqrsO));
		}
		return mqOpe.obj2Str(xpbisqrsO);
		
		
	}
	public XpbisqrsO addFnc(){
		XpbisqrsO  xpbmdldfO = bisQrsDao.addProc(xpbisqrsI);
		return xpbmdldfO;
	}
	public XpbisqrsO updateFnc(){
		XpbisqrsO  xpbmdldfO = bisQrsDao.updateProc(xpbisqrsI);
		return xpbmdldfO;
	}
	public XpbisqrsO deleteFnc(){
		XpbisqrsO  xpbmdldfO = bisQrsDao.deleteProc(xpbisqrsI);
		return xpbmdldfO;
	}
	public XpbisqrsO queryByConditionFnc(){
		XpbisqrsO  xpbmdldfO = bisQrsDao.queryByCondition(xpbisqrsI);
		return xpbmdldfO;
	}
	public XpbisqrsO queryQrsKeyListFnc(){
		XpbisqrsO  xpbmdldfO = bisQrsDao.queryQrsKeyList(xpbisqrsI);
		return xpbmdldfO;
	}
	public XpbisqrsO deleteByConditionFnc(){
		XpbisqrsO  xpbmdldfO = bisQrsDao.deleteByCondition(xpbisqrsI);
		return xpbmdldfO;
	}
	
	public XpbisqrsO errActionFnc(){
		XpbisqrsO  xpbisqrsO = new XpbisqrsO();
		xpbisqrsO.setTrx_id("XPLSTDAT");
		xpbisqrsO.setType_id("O");
		xpbisqrsO.setRtn_code("0000009");
		xpbisqrsO.setRtn_mesg("Invalid Action_flg");
		return xpbisqrsO;
	}
	
}
